//
//  KRStuffInfoCell.m
//  cookBook
//
//  Created by kuroken on 16/1/24.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRStuffInfoCell.h"
#import <PureLayout.h>
#import "KRConst.h"
#import "KRStuff.h"

static NSString * const KRStuffInfo = @"stuffInfo";

@interface KRStuffInfoCell ()
/** 食材名 label*/
@property (nonatomic, weak) UILabel *nameLabel;
/** 食材详情标识 imageView*/
@property (nonatomic, weak) UIImageView *flagImageView;
/** 食材重量 label */
@property (nonatomic, weak) UILabel *weightLabel;
/** 分隔线 */
@property (nonatomic, weak) UIView *lineView;
@end

@implementation KRStuffInfoCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    KRStuffInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:KRStuffInfo];
    if (!cell) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:KRStuffInfo];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupSubview];
        [self addConstraint];
    }
    return self;
}

- (void)setStuff:(KRStuff *)stuff
{
    _stuff = stuff;
    
    [self.nameLabel setText:stuff.stuff_name];
    [self.weightLabel setText:stuff.stuff_weight];
    [self.nameLabel setTextColor:[stuff.stuff_type isEqualToString:@"1"] ? [UIColor blackColor] : [UIColor grayColor]];
    if ([stuff.stuff_foodFlag isEqualToString:@"0"]) {
        self.flagImageView.hidden = YES;
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
    } else {
        self.flagImageView.hidden = NO;
        [self setSelectionStyle:UITableViewCellSelectionStyleDefault];
    }
    
}

- (void)setupSubview
{
    /** 食材名 label*/
    UILabel *nameLabel = [[UILabel alloc] init];
    [self.contentView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    /** 食材详情标识 imageView*/
    UIImageView *flagImageView = [[UIImageView alloc] init];
    [flagImageView setImage:[UIImage imageNamed:@"ico_info"]];
    flagImageView.size = flagImageView.image.size;
    [self.contentView addSubview:flagImageView];
    self.flagImageView = flagImageView;
    
    /** 食材重量 label */
    UILabel *weigthLabel = [[UILabel alloc] init];
    [weigthLabel setTextColor:[UIColor grayColor]];
    [self.contentView addSubview:weigthLabel];
    self.weightLabel = weigthLabel;
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = KRColorWithAlpha(188, 187, 192, 0.3);
    [self.contentView addSubview:lineView];
    self.lineView = lineView;

}

- (void)addConstraint
{
    [self.nameLabel autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
    [self.nameLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:23];
    
    [self.flagImageView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.nameLabel];
    [self.flagImageView autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.nameLabel withOffset:5];
    
    [self.weightLabel autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.nameLabel];
    [self.weightLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:KRScreenWidth * 0.5];
    
    [self.lineView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.nameLabel];
    [self.lineView autoSetDimensionsToSize:CGSizeMake(KRScreenWidth, 0.3)];
    [self.lineView autoPinEdgeToSuperviewEdge:ALEdgeBottom];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
